import React from "react";

import styles from "./index.module.css";

import NavHeader from "../../components/NavHeader";

import HouseItem from "../../components/HouseItem";

import NoHouse from "../../components/NoHouse";

import { API } from "../../utils/api";

import {BASE_URL} from '../../utils/url'
export default class Favorate extends React.Component {
  state = {
    isLogin: false,
    itemList: []
  };
  componentDidMount() {
    this.getItemList();
  }
  async getItemList() {
    const { data: res } = await API.get("/user/favorites");
    if (res.body.length !== 0) {
      this.setState(() => {
        return {
          itemList: res.body,
          isLogin: true
        };
      });
    }
  }
  getItem = () => {
    const { itemList } = this.state;
    if(itemList.length !== 0) { return itemList.map(item => (
        <HouseItem
          key={item.houseCode}
          tags={item.tags}
          desc={item.desc}
          src={BASE_URL + item.houseImg}
          title={item.title}
          price={item.price}
          onClick={() => {
            this.props.history.push(`/detail/${item.houseCode}`);
          }}
        />
      ))}
      return <NoHouse>您还没有收藏，去逛逛吧</NoHouse>
   
  };
  render() {
    return (
      <div className={styles.root}>
        <NavHeader>收藏</NavHeader>
         {this.getItem() }
      </div>
    );
  }
}
